<template>
  <div class="categories">
    <el-button class="addCategories" type="success" @click="editSho" plain>添加商品</el-button>

    <el-table
    :data = 'tableData'
    row-key = 'cat_id'
    style="width: 100%">
      <el-table-column prop="cat_name" label="分类名称"></el-table-column>
      <el-table-column prop="cat_deleted" label="是否有效">
        <template v-slot:default = "{ row }">
          {{row.cat_deleted = false ? '否' :' 是'}}
        </template>
      </el-table-column>
      <el-table-column prop="cat_level" label="排序"></el-table-column>
      <el-table-column prop="" label="操作">
        <template v-slot:default = '{row}'>
          <el-button size="small" type="primary" plain icon="el-icon-edit"></el-button>
          <el-button size="small" type="danger" plain icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 添加分类 -->
    <el-dialog
      title="添加分类"
      :visible.sync="editVisible"
      width="30%">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop="cat_pid">
         <el-cascader
          :options="options"
          v-model="ruleForm.cat_pid"
          clearable
          :props="props"></el-cascader>
         </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      pagenum: 1,
      pagesize: 5,
      tableData: [],
      total: 0,
      editVisible: false,
      ruleForm: {
        cat_name: '',
        cat_level: '',
        cat_pid: []
      },
      options: [],
      props: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getCategories() // 进入页面直接渲染
  },
  methods: {
    async getCategories () { // 绚染页面
      const { meta, data } = await this.$axios.get(`categories`, {
        params: {
          type: 3,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      // console.log(meta, data)

      if (meta.status === 200) {
        this.tableData = data.result
        this.total = data.total
      } else {
        this.$message.error(meta.msg)
      }
    },
    async editSho () {
      this.editVisible = true
      const { data, meta } = await this.$axios.get(`categories?type=2`)
      if (meta.status === 200) {
        this.options = data
        // console.log(this.options)
      } else {
        this.$message.error(meta.msg)
      }
    },
    async addCategory () {
      await this.$refs.ruleForm.validate()
      const { cat_pid: catPid } = this.ruleForm
      const { meta } = await this.$axios.post('categories', {
        cat_pid: catPid[catPid.length - 1] || 0,
        cat_name: this.ruleForm.cat_name,
        cat_level: catPid.length
      })
      if (meta.status === 201) {
        this.$message.success(meta.msg)
        this.editVisible = false
        this.$refs.ruleForm.resetFields()
      }
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.pagesize = 1
      this.getCategories()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.pagenum = val
      this.getCategories()
    }

  }
}
</script>

<style lang = 'less' scoped>
  .categories{
    .addCategories{
      margin-bottom: 20px;
    }
  }
</style>
